//
//  ProjectListViewController.m
//  wsgz
//
//  Created by huck on 2017/5/7.
//  Copyright © 2017年 BoingPay. All rights reserved.
//

#import "ProjectViewController.h"
#import "AppUtils.h"
#import "HomeManager.h"
#import "MJRefreshFooterView.h"
#import "MJRefreshHeaderView.h"
#import "ProjectListTableViewCell.h"

static NSString *s_projectSearchListCellIdentifier = @"ProjectSearchListCellIdentifier";

@interface ProjectViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDelegate>

@property (nonatomic, retain) MJRefreshFooterView *refreshFooterView;
@property (nonatomic, strong) MJRefreshHeaderView *refreshHeaderView;
@property (nonatomic) NSInteger pageNO;//分页

@property (nonatomic) BOOL hasLoad;//分页


@end

@implementation ProjectViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initSubview];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_searchTextField resignFirstResponder];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_searchTextField resignFirstResponder];
    [self initData];
}

- (void)dealloc
{
    [_refreshFooterView free];
    [_refreshHeaderView free];
}


-(void)initSubview
{    
    [_searchListTableView registerNib:[UINib nibWithNibName:@"ProjectListTableViewCell" bundle:nil] forCellReuseIdentifier:s_projectSearchListCellIdentifier];
    
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];//线
    head.backgroundColor = [UIColor colorWithString:COLOR_DBD9DE];
    _searchListTableView.tableHeaderView = head;
    __weak id weakSelf = self;
    _refreshFooterView = [MJRefreshFooterView footer];
    _refreshFooterView.scrollView = _searchListTableView;
    _refreshFooterView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView){
        [weakSelf pullUpRefresh];
    };
    _refreshHeaderView = [[MJRefreshHeaderView alloc] initWithScrollView:_searchListTableView];
    _refreshHeaderView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [weakSelf pullDownRefresh];
    };
}

- (void)initData
{
    _pageNO = 1;
    _refreshFooterView.stopRefresh = NO;
    [_refreshFooterView setStopRefresh:NO];
    [self requestData:YES showLoading:YES];
    
    if(!_refreshHeaderView || ![_refreshHeaderView isRefreshing]){
        [_searchListTableView setContentOffset:CGPointMake(0, 0)];
    }
}

- (void)requestData:(BOOL)reset showLoading:(BOOL)showLoading
{
    NSString *projectName = [AppUtils trim:_searchTextField.text];
    if (showLoading) {
        [Toast loading];
    }
    [[HomeManager sharedInstance] requestSearchProject:projectName typeId:_projectTypeId page:_pageNO reset:reset complete:^(BOOL success, BOOL hasMore) {
        _hasLoad = YES;
        if (_refreshFooterView) {
            [_refreshFooterView endRefreshing];
            [_refreshFooterView setStopRefresh:!hasMore];
        }
        if (showLoading) {
            [Toast loaded];
        }
        if (_refreshHeaderView) {
            [_refreshHeaderView endRefreshing];
        }
        if (!reset) {
            [_refreshFooterView endRefreshing];
        }
        if(hasMore){
            _pageNO++;
        }
        [_searchListTableView reloadData];
    }];
}

/**
 * 下拉刷新消息
 */
- (void)pullDownRefresh
{
    [self initData];
}

//上拉刷新
- (void)pullUpRefresh
{
    if ([AppUtils hasConnected:YES]) {
        [self requestData:NO showLoading:NO];
    }
    else {
        [_refreshFooterView endRefreshing];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
        [super textFieldShouldReturn:textField];
    [self initData];
    return YES;
}

// 用于自定义键盘的移动view （BaseViewController）
- (UIView *)frameRootView
{
    return self.view;
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self hideInput];
    // 滚动到底部，自动加载
    if([AppUtils isScrollToBottom:scrollView] && ![_refreshFooterView isRefreshing] && !_refreshFooterView.stopRefresh) {
        [_refreshFooterView beginRefreshing];
    }
}

#pragma mark UITableDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = [HomeManager sharedInstance].projectInfoList.count;
    _emptyView.hidden = count>0 || !_hasLoad;
    _searchListTableView.hidden = count==0;
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ProjectListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:s_projectSearchListCellIdentifier];
    if (nil == cell) {
        return nil;
    }
    
    ProjectInfoData *data = [[HomeManager sharedInstance].projectInfoList objectAtIndex:indexPath.row];
    
    [cell setInfo:data];
    return cell;
}




@end
